package com.hcb.tonguetiphealth.widget.home;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Handler;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hcb.tonguetiphealth.view.CityWideActivity;
import com.hcb.tonguetiphealth.view.DIYActivity;
import com.hcb.tonguetiphealth.view.HealthMirrorActivity;
import com.hcb.tonguetiphealth.view.HealthShareActivity;
import com.hcb.tonguetiphealth.view.HealthShareSearchActivity;
import com.hcb.tonguetiphealth.view.LoginActivity;
import com.hcb.tonguetiphealth.view.MemberActivity;
import com.hcb.tonguetiphealth.view.MoreActivity;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.view.RecommendHealthActivity;
import com.hcb.tonguetiphealth.view.ResBookActivity;
import com.hcb.tonguetiphealth.view.SearchActivity;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class HomeWidget extends GeneralWidget implements OnClickListener {
	
	private Activity ctx;
	private int width=0,height=0;
	private Button tbQuickSearch;

	private Handler handler;
	private Thread thread ;
	private ArrayList<ImageView> imageViews ;
	private ArrayList<String> imageUrls ;
	private ArrayList<Bitmap> bitmaps ;
	private LinearLayout llCity, llSearch, llSearch2, llHotRes, llLogin, llMore ;
	private LinearLayout llRes, lljkdr, llsjjk, lljkcd ;//, llDrink
	
	public HomeWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.widget_home, this); 
		
//		LctManager lctManager = new LctManager(ctx);
//		lctManager.getLoationByGPS();
		
		llCity = (LinearLayout)findViewById(R.id.linearlayout_home_city);
		llSearch = (LinearLayout)findViewById(R.id.linearlayout_home_search);
		llSearch2 = (LinearLayout)findViewById(R.id.linearlayout_home_search2);
		llHotRes = (LinearLayout)findViewById(R.id.linearlayout_home_res);
		llLogin = (LinearLayout)findViewById(R.id.linearlayout_home_login);
		llMore = (LinearLayout)findViewById(R.id.linearlayout_home_more);
		
		llRes = (LinearLayout)findViewById(R.id.linearlaout_home_restaurant_book);
		//llDrink = (LinearLayout)findViewById(R.id.linearlaout_home_drink_book);
		lljkdr = (LinearLayout)findViewById(R.id.home_linearlayout_jkdr);
		llsjjk = (LinearLayout)findViewById(R.id.home_linearlayout_sjjk);
		lljkcd = (LinearLayout)findViewById(R.id.home_linearlayout_jkcd);

    	llCity.setOnClickListener(this);
		llSearch.setOnClickListener(this);
		llSearch2.setOnClickListener(this);
		llHotRes.setOnClickListener(this);
		llLogin.setOnClickListener(this);
		llMore.setOnClickListener(this);
		
		if (llRes == null) {
			System.out.println("-nulllllllllllll");
		}
		llRes.setOnClickListener(this);
		//llDrink.setOnClickListener(this);
		lljkdr.setOnClickListener(this);
		llsjjk.setOnClickListener(this);
		lljkcd.setOnClickListener(this);

		bindTouchEvent(llRes);
		//bindTouchEvent(llDrink);
		bindTouchEvent(llSearch);
		bindTouchEvent(lljkdr);
		bindTouchEvent(llsjjk);
		bindTouchEvent(lljkcd);
		
		bottomMenuBindTouchEvent(llCity);
		bottomMenuBindTouchEvent(llSearch2);
		bottomMenuBindTouchEvent(llHotRes);
		bottomMenuBindTouchEvent(llLogin);
		bottomMenuBindTouchEvent(llMore);
		
        WindowManager manager = ctx.getWindowManager();
        width = manager.getDefaultDisplay().getWidth();
        height = manager.getDefaultDisplay().getHeight();
        
        imageViews = new ArrayList<ImageView>();
        imageUrls = new ArrayList<String>();
        bitmaps = new ArrayList<Bitmap>();
   
       
	}
	public void start(){
		showMessageDialog(ctx, "正在加载更新数据。。。");
		thread = new Thread(){
			public void run(){
//				startUpdateData();
			}
		};
		thread.start();
		
	}
	 
	 
	/**
	 * 加载顶部图片
	 */
	public void updateInfoImage(){
		
		if(imageUrls!=null && imageUrls.size()>0){
			Thread thread = new Thread(){
					public void run(){
						for(int i=0;i<imageUrls.size();i++){
							try {
					    		   URL picUrl = new URL(imageUrls.get(i));
					    		   Bitmap pngBM;
					    		   pngBM = BitmapFactory.decodeStream(picUrl.openStream());
					    		   pngBM = reSizeImage(pngBM, width*0.25,  width*0.25);
//					    		   imageViews.get(i).setImageBitmap(pngBM);
					    		   bitmaps.add(pngBM);
							} catch (IOException e) {
								Log.e("IOException", "HomeWidget.updateInfoImage"+e.getMessage());
//								e.printStackTrace();
							}
						}
			    		handler.post(runnableUi);   
					}
				};
				thread.start();
		}
	
	}
 
 
	
	/**
	 * 底部功能菜单按钮点击事件
	 */
	public void onClick(View v) {
		Intent intent = new Intent();
		switch(v.getId()) {
		case R.id.linearlayout_home_city:
			intent.setClass(ctx, DIYActivity.class);
			break;
		case R.id.linearlayout_home_search:
		//	intent.setClass(ctx, OfficeActivity.class);
//			ctx.setContentView(new ResearchWidget(ctx));
			//intent.setClass(ctx, SearchActivity.class);
			intent.setClass(ctx, CityWideActivity.class);
			break;
		case R.id.linearlayout_home_search2:
				intent.setClass(ctx, HealthShareSearchActivity.class);
				break;
		case R.id.linearlayout_home_res:
			intent.setClass(ctx, ResBookActivity.class);
			break;
		case R.id.linearlayout_home_login:
			intent.setClass(ctx, LoginActivity.class);
			break;
		case R.id.linearlayout_home_more:
			intent.setClass(ctx, MoreActivity.class);
			break;
		case R.id.linearlaout_home_restaurant_book:
			intent.setClass(ctx, HealthMirrorActivity.class);
			break;
		/*case R.id.linearlaout_home_drink_book:
			intent.setClass(ctx, DrinksBookActivity.class);
			break;*/
		case R.id.home_linearlayout_sjjk:
			intent.setClass(ctx, RecommendHealthActivity.class);
			break;
		case R.id.home_linearlayout_jkdr:
			intent.setClass(ctx, MemberActivity.class);
			break;
		case R.id.home_linearlayout_jkcd:
			intent.setClass(ctx, HealthShareActivity.class);
			break;
//		case R.id.button_home_health_send:
//			break;
//		case R.id.button_home_tongue_tip_health:
//			break;
//		case R.id.button_home_healthor:
//			break;
		default:
			break;
		}
		try {
			ctx.startActivity(intent);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**
	 * 圆形按钮触碰事件
	 */
	public void bindTouchEvent(final LinearLayout linearLayout) {
		linearLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					linearLayout.setBackgroundResource(R.drawable.circle_bg2);
				} else if (event.getAction() == KeyEvent.ACTION_UP) {
					switch (linearLayout.getId()) {
					case R.id.home_linearlayout_jkdr:
						linearLayout.setBackgroundResource(R.drawable.circle_bg);
						break;
					case R.id.linearlaout_home_restaurant_book:
						linearLayout.setBackgroundResource(R.drawable.circle_bg_green);
						break;
					case R.id.home_linearlayout_jkcd:
						linearLayout.setBackgroundResource(R.drawable.circle_bg2);
						break;
					case R.id.home_linearlayout_sjjk:
						linearLayout.setBackgroundResource(R.drawable.circle_bg_dullred);
						break;
					/*case R.id.linearlaout_home_drink_book:
						linearLayout.setBackgroundResource(R.drawable.circle_bg_purple);
						break;*/
					case R.id.linearlayout_home_search:
						linearLayout.setBackgroundResource(R.drawable.circle_bg_purple);
						break;
					default:
						break;
					}
					
				}
				
				return false;
			}
		});
	}
	
	private void bottomMenuBindTouchEvent(final LinearLayout llLayout){
		llLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					llLayout.setBackgroundResource(R.drawable.bottom_bg_black);
				} else {
					llLayout.setBackgroundResource(R.drawable.bottom_bg_none);
				}
				
				return false;
			}
		});
	}
	
	@Override
	public void notifyAdapter() {
		
	}
 
    
    public static Bitmap reSizeImage(Bitmap bgimage, double newWidth, double newHeight) { 
    		try {
    			// 获取这个图片的宽和高  
			    float width = bgimage.getWidth();  
		        float height = bgimage.getHeight();  
		        // 创建操作图片用的matrix对象  
		        Matrix matrix = new Matrix();  
		       // 计算宽高缩放率  
		        float scaleWidth = ((float) newWidth) / width;  
		        float scaleHeight = ((float) newHeight) / height;  
		        // 缩放图片动作  
		        matrix.postScale(scaleWidth, scaleHeight);  
		        Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,  
		                       (int) height, matrix, true);  
		        return bitmap;  
			} catch (Exception e) {
			}
    		return null;
    }  
    
	// 指引页面更改事件监听器
	class GuideInfoPageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int arg0) {
			System.out.println("------ GuidePageChangeListener : " + arg0);
		//	HomeInfo info = infoList.get(arg0);
			switch(arg0) {
			case 0:
				
				break;
			case 1: 
				break;
			case 2: 
				break;
			case 3: 
				break;
			case 4: 
				break;
			}
		}
	}
	// 指引页面更改事件监听器
	class GuideTipsPageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int arg0) {
			//HomeNotice notice = noticeList.get(arg0);
			switch(arg0) {
			case 0:
				break;
			case 1: 
				break;
			case 2: 
				break;
			}
		}
	 }
	 Runnable runnableUi = new Runnable(){
		public void run() {
			try {
				if(imageViews!=null && bitmaps!=null&& bitmaps.size()>0 && imageViews.size()>0) {
					for(int i=0; i<imageUrls.size(); i++){
						imageViews.get(i).setImageBitmap(bitmaps.get(i));
					}
				}
			} catch (Exception e) {
				Log.e("Exception", "HomeWidget.runableUi",e);
			}
			
		}
	};
 
}
